const urlParams = new URLSearchParams(window.location.search);
let pid=urlParams.get('id')
const loginName=window.sessionStorage.getItem("loginName")
const numReg=/^([1-9]{1}[0-9]{0,7})$/
const vm=new Vue({
    el:'#detail',
    data:{
        product:{
          id:'',
          name:'',
          description:'',
          price:'',
          stock:'',
          categoryLevel1Id:'',
          categoryLevel2Id:'',
          categoryLevel3Id:'',
          fileName:'',
        },
        imgStr:'http://192.168.2.106:8848/images/',
        productNum:1
    },
    methods:{
        getProductById()
        {
            axios.get('/api/admin/getProductById',{
                params:{
                    id:pid
                }
            }).then(rs=>{
                this.product=rs.data.product
                console.log(this.product)
            })
        },
        addIntroCar(product)
        {
            if(confirm("是否加入购物车？"))
            {
                const formData=new FormData()
                let littlePrice=product.price*this.productNum
                let pro={id:product.id,name:product.name,price:product.price,fileName:product.fileName,num:this.productNum,
                    numPrice:littlePrice,stock:product.stock}
                formData.append("pro",JSON.stringify(pro))
                formData.append("loginName",loginName)
                axios.post('/api/car/addIntoCar',
                    formData
                ).then(rs=>{
                    if(rs.data.isAdd===true)
                    {
                        alert("加入购物车成功")
                        vm2.car=rs.data.car
                    }else{
                        alert("库存不足")
                    }
                })
            }
        },
        checkNum(stock)
        {
            if(isNaN(this.productNum))
            {
                this.productNum=1
            }
            if(!numReg.test(this.productNum))
            {
                this.productNum=1
            }
            this.checkTest(stock)
        },
        addNum(stock)
        {
            this.productNum++
            this.checkTest(stock)
        },
        deNum(stock)
        {
            this.productNum--
            this.checkTest(stock)
        },
        checkTest(stock)
        {
            if(this.productNum<=0)
            {
                this.productNum=1
            }
            if(this.productNum>=stock)
            {
                this.productNum=stock
            }
        }
    },
    created()
    {
        this.getProductById()
    }
})

const vm2=new Vue({
    el:'#top',
    data:{
        name:'',
        carDetails:[],
        imgStr:'http://192.168.2.106:8848/images/',
        car:{}
    },
    methods:{
        btn_sousuo()
        {
            id=null
            vm.name=this.name
            vm.getEsPageInfo(1,16)
        },
        getCarFromRedis()
        {
            axios.get("/api/car/getCar",{
                params:{
                    loginName
                }
            }).then(rs=>{
                this.car=rs.data.car
                console.log(this.car)
            })
        }
    },
    created() {
        this.getCarFromRedis()
    }
})
